import { post } from '@/utils/request';

interface Messages {
  role: 'user' | 'assistant' | 'system';
  content: string;
}
export const getMessage = async (messages: Messages) => {
  return post('/openai/chat/completions/search', {
    messages: [messages],
    window_id: await getWindowId(),
  });
};
// 获取窗口列表
export const getWindowList = () =>
  post('/openai/chatWindow/list').then((res: any) => {
    return res.result.records;
  });
// 创建窗口
export const createWindow = () =>
  post('/openai/chatWindow/add').then((res: any) => {
    return res.result;
  });

// 获取窗口id
let windowId = '';
export const getWindowId = async () => {
  if (windowId) return windowId;
  const list = await getWindowList();
  if (list.length) {
    windowId = list[list.length - 1].id;
    return windowId;
  }
  windowId = await createWindow();
  return windowId;
};
